/*
 * @Date: 2022-02-11 16:33:36
 * @LastEditors: tich425
 * @LastEditTime: 2022-02-11 16:37:47
 * @FilePath: \TodoList\utils\email.go
 * @Description: 发送验证码
 */
package utils

import (
	"fmt"
	"log"
	"net/smtp"
	"todo-list/global"

	"github.com/jordan-wright/email"
)

/**
 * @description: 发送邮箱验证码
 * @param {string} to
 * @param {string} code
 */
func SendEmail(to string, code string) bool {
	// 构造邮件内容
	e := &email.Email{
		From:    fmt.Sprintf("%v <%v>", global.CONFIG.Email.Name, global.CONFIG.Email.Addr),
		To:      []string{to},
		Subject: "邮箱验证",
		Text:    []byte("【TodoList】验证码: " + code + ", 有效期 15 分钟"),
	}

	// 发送邮件
	if err := e.Send(global.CONFIG.Email.Smtp, smtp.PlainAuth("", global.CONFIG.Email.Addr, global.CONFIG.Email.Password, global.CONFIG.Email.Host)); err != nil {
		log.Print("Email send error: ", err)
		return false
	}
	return true
}
